package com.lwl.controller.sas;


import com.github.pagehelper.PageInfo;
import com.lwl.entity.LayData;
import com.lwl.entity.MyPage;
import com.lwl.entity.sas.TQuestionSas;
import com.lwl.service.TQuestionSasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("tQuestionSas")
public class TQuestionSasController {
    @Autowired
    private TQuestionSasService tQuestionSasService;
    @Resource
    private TQuestionSasService fileService;

    @GetMapping("queryAll")
    @ResponseBody
    public LayData queryAll(MyPage myPage, TQuestionSas tQuestionSas) {
        PageInfo tQuestionSass = tQuestionSasService.queryAll(myPage, tQuestionSas);
        return new LayData(tQuestionSass);
    }
    @PostMapping("registerQuestion")
    @ResponseBody
    public LayData registerQuestion(TQuestionSas tQuestionSas){
        boolean status = tQuestionSasService.insertQuestion(tQuestionSas);
        return new LayData(status);
    }
    @PostMapping("queryByName")
    @ResponseBody
    public LayData queryByName(String question){
        boolean status = tQuestionSasService.queryQuestion(question);
        return new LayData(status);
    }

    @PostMapping("deleteRole")
    @ResponseBody
    public LayData deleteRole(Integer id){
        boolean status = tQuestionSasService.deleteQuestion(id);
        return new LayData(status);
    }

    /**
     * 批量导入题库
     * @param file
     * @return
     */
    @PostMapping("uploadInfo")
    @ResponseBody
    public LayData uploadInfo(MultipartFile file){
        return new LayData(tQuestionSasService.uploadExcel(file));
    }

    @GetMapping("allQuestion")
    public String allQuestion(MyPage myPage, HttpServletRequest request){
        myPage.setLimit(100);
        myPage.setPage(1);
        PageInfo all = tQuestionSasService.getAll(myPage);
        request.setAttribute("allSub",all);
        System.out.println("查询到所有的题目信息为all = " + all);
        return "sas/index";
    }

    @PostMapping(value = "/import")
    @ResponseBody
    public LayData storageService(MultipartFile file) {
        boolean status =  fileService.dealFile(file);
        return new LayData(status);
    }

}
